//
//  LLCollection.swift
//  LaiLang
//
//  Created by David on 2017/2/4.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class LLCollection: UIView {

    var collection: UICollectionView!
    
//    init(point: CGPoint, eachSize: CGSize, num: Int) {
//        super.init(frame: CGRect(x: point.x, y: point.y, width: eachSize.width * 2, height: eachSize.height * CGFloat(num/2 + 1)))
//    }
    
    var items: [UIButton] = []
    
    var itemsData: [(icon: String, text: String)] = []
    
    var square : (row: Int, column: Int) = (0, 0)
    
    var borders: [UIView]?
    
    init(frame: CGRect, row:Int, column: Int) {
        super.init(frame: frame)
        square = (row, column)
        let itemWidth = frame.width / CGFloat(column)
        let itemHeight = frame.height / CGFloat(row)
        for i in 0..<row{
            for j in 0..<column{
                let btn = UIButton(frame: CGRect(x: itemWidth * CGFloat(j), y: itemHeight * CGFloat(i), width: itemWidth, height: itemHeight))
                btn.setTitleColor(UIColor.black, for: .normal)
                btn.contentMode = .center
                items.append(btn)
                itemsData.append(("", ""))
                addSubview(btn)
                
            }
        }
    }
    
    func setItemTitle(at: Int, text: String){
        if at < 0 || at >= items.count {
            return
        }
        var icon = itemsData[at].icon
        if icon == "" {
            icon = "?"
        }
        let btnText = "\(icon)\n\(text)"
        itemsData[at] = (icon, text)
        let attrStr = NSMutableAttributedString(string: btnText)
        
        attrStr.addAttribute(NSFontAttributeName, value: UIFont(name: "iconfont", size: 30)!, range: NSMakeRange(0, 1))
        attrStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(2, text.characters.count))
        items[at].setAttributedTitle(attrStr, for: .normal)
        items[at].titleLabel?.numberOfLines = 0
        return
    }
    
    func setItemIcon(at: Int, iconUnicode: String){
        if at < 0 || at >= items.count {
            return
        }
        var text = itemsData[at].text
        if text == "" {
            text = "Item\(at)"
        }
        let btnText = "\(iconUnicode)\n\(text)"
        itemsData[at] = (iconUnicode, text)
        let attrStr = NSMutableAttributedString(string: btnText)
        
        attrStr.addAttribute(NSFontAttributeName, value: UIFont(name: "iconfont", size: 30)!, range: NSMakeRange(0, 1))
        attrStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(2, text.characters.count))
        
        items[at].setAttributedTitle(attrStr, for: .normal)
        items[at].titleLabel?.numberOfLines = 0
    }
    
    func setCoverImage(at: Int, image: UIImage?){
        if at < 0 || at >= items.count {
            return
        }
        items[at].setTitle("", for: .normal)
        items[at].setImage(image, for: .normal)
    }
    
    func setImage(at: Int, image: UIImage?){
        if at < 0 || at >= items.count {
            return
        }
        items[at].setImage(image, for: .normal)
        
    }
    
    func setSingleTitle(at: Int, title: String?){
        if at < 0 || at >= items.count {
            return
        }
        items[at].setTitle(title, for: .normal)
    }
    
    func setInnerBorder(borderWeight: CGFloat, color: UIColor){
        let itemWidth = frame.width / CGFloat(square.column)
        let itemHeight = frame.height / CGFloat(square.row)
        if borders != nil {
            for v in borders!{
                v.removeFromSuperview()
            }
        }
        borders = []
        for i in 1...square.row-1 {
            let border = UIView(frame: CGRect(x: 0, y: itemHeight * CGFloat(i) - borderWeight, width: frame.width, height: borderWeight))
            border.backgroundColor = color
            borders?.append(border)
            addSubview(border)
            
        }
        for i in 1...square.column-1 {
            let border = UIView(frame: CGRect(x: itemWidth * CGFloat(i) - borderWeight, y: 0, width: borderWeight, height: frame.height))
            border.backgroundColor = color
            borders?.append(border)
            addSubview(border)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
